﻿using DotNetExtensions.Core.IOC;
using DotNetExtensions.Core.MVC;
using DotNetExtensions.Core.Restful;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace DotNetExtensions.Core.SqlSugar
{
    [Inject(Scope = InjectScope.Scoped)]
    [AutoInject]
    public class SimpleService<TModel, TKey, TSession, TSearchModel> : BaseService<TModel, TSession> where TModel : class, new() where TSearchModel : SearchModel where TSession : class, new()
    {
        public new SimpleRepository<TModel, TKey> Repository;
        public SimpleRepository<TEntity, int> SimpleRepository<TEntity>() where TEntity : class, new()
        {
            return Autofac.RegistTypeResolver.Default.Resolve<SimpleRepository<TEntity, int>>();
        }

        public SimpleService(Session<TSession> session, SimpleRepository<TModel,TKey> simpleRepository) : base(session, simpleRepository)
        {
            Repository = simpleRepository;
        }

        public ISimpleClient<TModel> Model => Repository.Model;

        public virtual TModel GetById(TKey key)
        {
            return Repository.GetById(key);
        }
        public virtual bool Insert(TModel entity)
        {
            return Repository.Insert(entity);
        }
        public virtual bool Insert(List<TModel> entitys)
        {
            return Repository.Insert(entitys);
        }
        public virtual bool Delete(TKey key)
        {
            return Repository.Delete(key);
        }
        public virtual bool Update(TModel entity)
        {
            return Repository.Update(entity);
        }
        public virtual bool Update(List<TModel> entitys)
        {
            return Repository.Update(entitys);
        }
        public virtual bool Exists(TKey key)
        {
            return Repository.Exists(key);
        }
        public virtual bool Exists(Expression<Func<TModel, bool>> expression)
        {
            return Repository.Exists(expression);
        }
        public virtual List<TModel> GetAll()
        {
            return Repository.GetAll();
        }
        public virtual List<TModel> GetList(TSearchModel search)
        {
            return Repository.GetAll();
        }
        public virtual PageResult<TModel> GetPageList(QueryModel<TSearchModel> query)
        {
            return Repository.GetPageList(query.PageIndex, query.PageSize);
        }
    }
}
